package com.hzncc.scaffold.controller.Rear.Other.Upload;

import cn.hutool.core.collection.CollUtil;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Vo.AttachFileVo;
import com.hzncc.scaffold.entity.Vo.ManageUploadVo;
import com.hzncc.scaffold.service.ISysFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: DrunkBear
 * @Date: 2022-07-08 14:23
 * @Description: null
 */
@Api(tags = "后台管理-上传相关接口")
@RequestMapping("/manage/upload")
@RestController
public class UploadController {


    @Autowired
    ISysFileService sysFileService;

    @PostMapping
    @ApiOperation("上传文件")
    public ResultT upload(MultipartHttpServletRequest request) {


        MultiValueMap<String,MultipartFile> map = request.getMultiFileMap();// 为了获取文件，这个类是必须的
        List<MultipartFile> list = map.get("files[]");// 获取到文件的列表
        if(CollUtil.isEmpty(list)){
            list =  map.get("file");
            if(CollUtil.isEmpty(list)){
                list =  map.get("files");
            }
        }
        try {
            ManageUploadVo manageUploadVo = new ManageUploadVo();
            ArrayList<AttachFileVo> docUrl = new ArrayList<>();
            ArrayList<String> imgUrl = new ArrayList<>();
            ArrayList<AttachFileVo> videoUrl = new ArrayList<>();
//            ArrayList<AttachFileVo> zipUrl = new ArrayList<>();
//            ArrayList<AttachFileVo> pptUrl = new ArrayList<>();
//            ArrayList<AttachFileVo> excelUrl = new ArrayList<>();


            for (MultipartFile multipartFile : list) {
                AttachFileVo attachFileVo = new AttachFileVo();

                String fileName = multipartFile.getOriginalFilename();
                String prefix = fileName.substring(fileName.lastIndexOf("."));
                if (".doc".equals(prefix) || ".docx".equals(prefix) || ".pdf".equals(prefix)||
                        ".xls".equals(prefix) || ".xlsx".equals(prefix)||
                        ".zip".equals(prefix) || ".rar".equals(prefix) || ".arj".equals(prefix)|| ".lzh".equals(prefix)|| ".jar".equals(prefix)||
                        ".ppt".equals(prefix) || ".pptx".equals(prefix)||
                        ".xls".equals(prefix) || ".xlsx".equals(prefix)
                ) {
                    String url = sysFileService.uploadFileMinio(multipartFile);
                    attachFileVo.setFilename(multipartFile.getOriginalFilename());
                    attachFileVo.setPath(url);

                    docUrl.add(attachFileVo);

                    continue;
                }

                else if (".jpg".equals(prefix) || ".png".equals(prefix)) {
                    String url = sysFileService.uploadFileMinio(multipartFile);
                    attachFileVo.setFilename(multipartFile.getOriginalFilename());
                    attachFileVo.setPath(url);
                    imgUrl.add(url);

                    continue;
                }

                else if (".mp4".equals(prefix) || ".mp3".equals(prefix)|| ".avi".equals(prefix)|| ".rmvb".equals(prefix)|| ".wmv".equals(prefix)) {
                    String url = sysFileService.uploadFileMinio(multipartFile);
                    attachFileVo.setFilename(multipartFile.getOriginalFilename());
                    attachFileVo.setPath(url);
                    videoUrl.add(attachFileVo);
                }else {
                    String url = sysFileService.uploadFileMinio(multipartFile);
                    attachFileVo.setFilename(multipartFile.getOriginalFilename());
                    attachFileVo.setPath(url);
                    docUrl.add(attachFileVo);
                }

//
//                if (".zip".equals(prefix) || ".rar".equals(prefix) || ".arj".equals(prefix)|| ".lzh".equals(prefix)|| ".jar".equals(prefix)) {
//                    String url = sysFileService.uploadFileMinio(multipartFile);
//                    attachFileVo.setFilename(multipartFile.getOriginalFilename());
//                    attachFileVo.setPath(url);
//                    zipUrl.add(attachFileVo);
//                }
//
//                if (".ppt".equals(prefix) || ".pptx".equals(prefix)) {
//                    String url = sysFileService.uploadFileMinio(multipartFile);
//                    attachFileVo.setFilename(multipartFile.getOriginalFilename());
//                    attachFileVo.setPath(url);
//                    pptUrl.add(attachFileVo);
//                }
//
//                if (".xls".equals(prefix) || ".xlsx".equals(prefix)) {
//                    String url = sysFileService.uploadFileMinio(multipartFile);
//                    attachFileVo.setFilename(multipartFile.getOriginalFilename());
//                    attachFileVo.setPath(url);
//                    excelUrl.add(attachFileVo);
//                }
            }
            manageUploadVo.setDocUrl(docUrl);
            manageUploadVo.setImgUrl(imgUrl);
            manageUploadVo.setVideoUrl(videoUrl);
//            manageUploadVo.setZipUrl(zipUrl);
//            manageUploadVo.setPptUrl(pptUrl);
//            manageUploadVo.setExcelUrl(excelUrl);
            return ResultT.success(manageUploadVo);
        } catch (Exception exception) {
            exception.printStackTrace();
            return ResultT.fail(exception.getMessage());
        }
    }
}
